package com.xiaobai.SynchrobizedLock;

import lombok.extern.slf4j.Slf4j;
import org.openjdk.jol.info.ClassLayout;

/*
 * biased锁
 * -XX:BiasedLockingStartupDelay=0 -XX:-TieredCompilation -XX:+UseBiasedLocking
 */
@Slf4j
public class BiasedLocks2 {
    public static void main(String[] args) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        Dog dog = new Dog();
        new Thread(() -> {
            log.debug(ClassLayout.parseInstance(dog).toPrintable());
            synchronized (dog){
                //这个时候加的是偏向锁
                log.debug(ClassLayout.parseInstance(dog).toPrintable());
            }
            //偏向锁默认是延时加载的
            log.debug(ClassLayout.parseInstance(dog).toPrintable());
            synchronized (BiasedLocks2.class){
                BiasedLocks2.class.notify();
            }
        }, "t1").start();
        new Thread(() -> {
            synchronized (BiasedLocks2.class){
                try {
                    BiasedLocks2.class.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            log.debug(ClassLayout.parseInstance(dog).toPrintable());
            //加的就是轻量锁
            synchronized (dog){
                log.debug(ClassLayout.parseInstance(dog).toPrintable());
            }
            //偏向锁默认是延时加载的
            log.debug(ClassLayout.parseInstance(dog).toPrintable());
        }, "t2").start();

    }
}

